package 尚硅谷;

import java.util.concurrent.TimeUnit;

/**
 * @author zhangmin
 * @create 2022-03-24 20:17
 */
class HoldLockThread implements Runnable{
    private String LockA;
    private String LockB;

    public HoldLockThread(String lockA, String lockB) {
        LockA = lockA;
        LockB = lockB;
    }

    @Override
    public void run() {
        synchronized (LockA){
            System.out.println(Thread.currentThread().getName()+"\t自己持有："+LockA+"\t尝试获得："+LockB);

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LockB){
                System.out.println(Thread.currentThread().getName()+"\t自己持有："+LockB+"\t尝试获得："+LockA);
            }
        }

    }
}
public class DeadLockDemo {
    public static void main(String[] args) {
        String LockA="LockA",LockB="LockB";
        new Thread(new HoldLockThread(LockA,LockB),"ThreadAAA").start();
        new Thread(new HoldLockThread(LockB,LockA),"ThreadBBB").start();
    }
}
